/* File Name: EventObserver.java */
//Jose Heitor

package com.chenwenxuan.sync.event;

import java.util.Observable;
import java.util.Observer;                       // This is the required Event Handler Interface

// This is a proxy class that receives 'raw' notifications from the dispatcher of a particular event.
// It then formats the event notification and forwards it to the listener object registered with this
// Observer to be notified of this event.
class EventObserver implements Observer
{
    // This is a reference to the object that will actually handle this event
    private final IEventHandler handler;
    
    // This is the name of this event
    private final String event;
    
    // Sole constrauctor. Requires the reference to the listener object and the event name
    public EventObserver(IEventHandler handler, String event)
    {
      this.handler = handler;
      this.event = event;
    }
    
    // Returns a reference to the object handling this event
    public IEventHandler getTarget() { return handler; }
    
    // Returns the name of the event that this object is managing
    public String getEvent() { return event; }
    
    // This is the only method defined in the Observer Interface - it is called from the 
    // EventDispatcher object that is dispatching notifications of this event to all registered
    // Observers. The implementation logic forwards the notification to the ultimate listener
    // object that registered this Observer to handle and forward notification of this event.
    public void update (Observable obj, Object arg)
    {
        // Must instantiate an object reference to be able to cast the 'obj' parameter to its
        // source EventDispatcher object... so that it can call the 'getSource()' method to
        // obtain a reference to the actual object that generated the event.
        EventDispatcher ev;
        ev = (EventDispatcher)obj;
            
        handler.handleEvent(ev.getSource(), event, arg);
    }
}
